package cn.sfrog.project.biz.applet.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author chen
 * @Date 2023-11-27 11:44
 * @title WechatUtil
 * @description
 **/
public class WechatUtil {
    /**
     * 获取OpenIDAndSessionKey
     * @param code
     * @param appId
     * @param appSecret
     * @param grantType
     * @return
     */
    @Autowired
    private static  RestTemplate restTemplate;
    public static JSONObject getSessionKeyOrOpenId(String code,String appId,String appSecret,String grantType) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String,String> requestUrlParam = new HashMap<>();
        // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&amp;token=164113089&amp;lang=zh_CN
        //小程序appId
        requestUrlParam.put("appid", appId);
        //小程序secret
        requestUrlParam.put("secret", appSecret);
        //小程序端返回的code
        requestUrlParam.put("js_code", code);
        //默认参数
        requestUrlParam.put("grant_type", grantType);
        //发送post请求读取调用微信接口获取openid用户唯一标识
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam));
        return jsonObject;
    }

    /**
     * 获取手机号
     * @param code
     * @param
     * @return
     */
    public static JSONObject getPhoneNumber(String code,String accessToken) {
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("code",code);
        String res = HttpUtil.post(url,JSONObject.toJSONString(map));
        JSONObject phoneJson = JSONObject.parseObject(res);
        return phoneJson;
    }
    public static JSONObject getAccessToken(String appid,String  secret) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
        Map<String,String> requestUrlParam = new HashMap<>();
        //小程序appId
        requestUrlParam.put("grant_type", "client_credential");
        //小程序appId
        requestUrlParam.put("appid", appid);
        requestUrlParam.put("secret", secret);
        //发送post请求
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam));
        return jsonObject;
    }

    public static ResponseEntity<Object> testPhone(String code){
//        String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", "wxc2056bd8795a003f", "e741c5b3097019c87473f678af7b823c");
//        JSONObject token = JSON.parseObject(HttpUtil.get(tokenUrl));

        //  通过token和code来获取用户手机号
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + "75__ihP15qk33HO6nz42J2vJN16hduaHpWhxcgnOZWRAJ6_zvuswf98VVV36bzrF4Dg4s8ulC45ls0ceUElN3t6QVYpLjXuh5McK9tAtHQdJtMpBiDEyd2QEedE79YAGGbAEAEKK";

        //  封装请求体
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("code", code);

        //封装请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap,headers);

        //通过RestTemplate发送请求，获取到用户手机号码
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class);
//        System.out.println(response);
        return response;
    }

}
